package _0713_Subarray_Product_Less_Than_K;

/**
 * Date: 2021/9/23 9:49
 * Content:
 * 执行用时：5 ms, 在所有 Java 提交中击败了41.86%的用户
 * 内存消耗：45.2 MB, 在所有 Java 提交中击败了76.18%的用户
 */
public class Solution {

    public int numSubarrayProductLessThanK(int[] nums, int k) {
        int res = 0;
        for (int i = 0, j = 0, p = 1; i < nums.length; i++) {
            p *= nums[i];
            while (j <= i && p >= k) p /= nums[j++];
            res += i - j + 1;
        }
        return res;
    }

    public static void main(String[] args) {

        int[] nums = {10, 5, 2, 6};
        System.out.println((new Solution()).numSubarrayProductLessThanK(nums, 100));  // 8
    }
}
